<?php
class Touroperatorskoeffs_model extends CRM_Model{
	private $typeDocRid;
	public function __construct(){
		parent::__construct();
		$typeDocRow = $this->GetTypeDoc();
		$this->typeDocRid = $typeDocRow->rid; 
	}
	
	public function GetDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_touroperators_koeff_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_touroperators_koeff_rows._touroperators_rid as _touroperators_rid,
							_touroperators_koeff_rows.koeff_value as koeff_value,
							_touroperators_koeff_rows._currencies_rid as _currencies_rid,
							_currencies.code as currency_code,
							DATE_FORMAT(_touroperators_koeff_rows.bdate, \'%d.%m.%Y\') as bdate,
							_touroperators.touroperator_name as touroperator_name,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\')  as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_touroperators_koeff_headers', '_touroperators_koeff_headers._documents_rid = _documents.rid');
		$this->db->join('_touroperators_koeff_rows', '_touroperators_koeff_rows._touroperators_koeff_headers_rid = _touroperators_koeff_headers.rid');
		$this->db->join('_touroperators', '_touroperators_koeff_rows._touroperators_rid = _touroperators.rid');
		$this->db->join('_currencies', '_touroperators_koeff_rows._currencies_rid = _currencies.rid');
		$this->db->where(array('_documents._documents_types_rid'=>$this->typeDocRid));
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetTouroperatorsList(){
		$this->db->select('_touroperators.rid, _touroperators.touroperator_name as touroperator_name');
		$this->db->from('_touroperators');
		$this->db->orderby('touroperator_name');
		$query = $this->DBGet('_touroperators');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
		
	public function GetEditDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_touroperators_koeff_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_touroperators_koeff_rows._touroperators_rid as _touroperators_rid,
							_touroperators_koeff_rows.koeff_value as koeff_value,
							_touroperators_koeff_rows._currencies_rid as _currencies_rid,
							DATE_FORMAT(_touroperators_koeff_rows.bdate, \'%d.%m.%Y\') as bdate,
							_touroperators.touroperator_name as touroperator_name,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\')  as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_touroperators_koeff_headers', '_touroperators_koeff_headers._documents_rid = _documents.rid');
		$this->db->join('_touroperators_koeff_rows', '_touroperators_koeff_rows._touroperators_koeff_headers_rid = _touroperators_koeff_headers.rid');
		$this->db->join('_touroperators', '_touroperators_koeff_rows._touroperators_rid = _touroperators.rid');
		$this->db->join('_currencies', '_touroperators_koeff_rows._currencies_rid = _currencies.rid');
		$this->db->where(array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->insert('_documents', $insDocumentArray);
		$docRid = $this->db->insert_id();
		$insHeaderArray = array('date_doc'=>$this->toMySQLDate($_POST['date_doc']),
							'_documents_rid'=>$docRid,
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);

		$this->db->insert('_touroperators_koeff_headers', $insHeaderArray);
		$insRowsArray = array('_touroperators_koeff_headers_rid'=>$this->db->insert_id(),
							'_touroperators_rid'=>$_POST['_touroperators_rid'],
							'_currencies_rid'=>$_POST['_currencies_rid'],
							'koeff_value'=>$_POST['koeff_value'],
							'bdate'=>$this->toMySQLDate($_POST['bdate']),
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->insert('_touroperators_koeff_rows', $insRowsArray);
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $docRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->update('_documents', $updateDocumentArray, array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
				
		$updateHeaderArray = array(
								'archive'=>$_POST['archive'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);

		$this->db->update('_touroperators_koeff_headers', $updateHeaderArray, array('_touroperators_koeff_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->db->getwhere('_touroperators_koeff_headers', array('_touroperators_koeff_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		if(!$query->num_rows()) {
			$this->db->trans_rollback();
    		return False;
		}
		$row = $query->row();
		$updateRowsArray = array('_touroperators_rid'=>$_POST['_touroperators_rid'],
							'_currencies_rid'=>$_POST['_currencies_rid'],
							'koeff_value'=>$_POST['koeff_value'],
							'bdate'=>$this->toMySQLDate($_POST['bdate']),
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->update('_touroperators_koeff_rows', $updateRowsArray, array('_touroperators_koeff_rows._touroperators_koeff_headers_rid'=>$row->rid));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $this->ciObject->GetSessionData('activerecord');
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_documents', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function GetTouroperatorKoeff($p_touroperatorRid, $p_currencyRid, $p_date = null){
		if(!$p_date) $p_date = date('Y-m-d');
		$this->db->select('_touroperators_koeff_rows.*');
		$this->db->from('_touroperators_koeff_rows');
		$this->db->where(array('_touroperators_rid'=>$p_touroperatorRid, 'bdate<='=>$p_date, '_currencies_rid'=>$p_currencyRid));
		$this->db->orderby('bdate');
		$this->db->limit('1');
		$query = $this->db->get();
		if($query->num_rows()) return $query->row();
		return false;
	}
	
}
?>